import { GetMaterialDetail, AddCart } from "~/api";

export const useContent = () => {
    const userStore = useUserStore();
    const dialogStore = useDialogStore();
    const detail = ref({});
    const route = useRoute();
    const cartSuccessRef = ref(null);

    const GetDetail = async () => {
        let id = route.params.id;
        const res = await GetMaterialDetail({
            id,
        });
        if (res.code == 200) {
            detail.value = res.data;
        }
    };

    const onAddCart = async () => {
        if (userStore.isLogin) {
            if (detail.value.is_cart) return false;

            const res = await AddCart({
                store_id: detail.value.id,
            });

            if (res.code == 200) {
                detail.value.is_cart = true;
                cartSuccessRef.value.show();
            } else {
                Message().warning(res.msg);
            }
        } else {
            LoginModal();
        }
    };

    const onShare = () => {
        dialogStore.isShareTemplateDialogVisible = true;
        dialogStore.shareUrl = location.href;
        dialogStore.shareTitle = "素材链接";
        dialogStore.shareName = detail.value.name;
    };

    return {
        cartSuccessRef,
        detail,
        GetDetail,
        onAddCart,
        onShare,
    };
};
